package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.OrderQueryPageBean;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Order;
import cn.itcast.service.OrderListService;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
@Slf4j
@RestController
@RequestMapping(path = "/order")
@CrossOrigin
public class OrderListController {

    @Reference
    private OrderListService orderListService;

    /**
     * 查询所有预约信息的人
     * @return
     */
    @GetMapping(path = "/findPage")
    public Result queryOrderAll(OrderQueryPageBean orderQueryPageBean){
        try {
            PageResult pageResult = this.orderListService.orderFindAll(orderQueryPageBean);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    /**
     * 新增预约
     * @param param
     * @return
     */
    @PostMapping("/submit")
    public Result saveOrder(@RequestBody Map<String,String> param){
        try {
            param.put("orderType",Order.ORDERTYPE_TELEPHONE);
            Boolean result = this.orderListService.saveOrder(param);
            return new Result(true,MessageConstant.ORDER_SUCCESS,result);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.ORDER_FAIL);
        }
    }

    /**
     * 根据编辑窗口查询对应id的数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result queryOrderById(@PathVariable("id")String id){
        try {
            Order order = this.orderListService.queryOrderById(id);
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,order);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    /**
     * 根据用户id修改就诊状态
     * @return
     */
    @PostMapping("/button/{id}/{arrival}")
    public Result updateButton (@PathVariable("id")String id,
                                @PathVariable("arrival")Integer arrival){
        try {
            Boolean result = this.orderListService.updateOrderArrival(id, arrival);
            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
    }
}
